package com.yty.controller;

import com.yty.annotation.LogOperation;
import com.yty.page.PageResult;
import com.yty.pojo.Emp;
import com.yty.param.EmpQueryParam;
import com.yty.pojo.Result;
import com.yty.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result findEmpList(EmpQueryParam param)
    {
        log.info("查询员工列表，参数：{}",param);
        PageResult empList = empService.findEmpList(param);
        return Result.success(empList);
    }

    @LogOperation
    @PostMapping
    public Result addEmp(@RequestBody Emp emp)
    {
        if (emp.getName()==null||emp.getName().equals("")
        || emp.getUsername()==null||emp.getUsername().equals("")
        ||emp.getGender()==null||emp.getPhone()==null||emp.getPhone().equals("")){
            return Result.error("参数异常！");
        }
        log.info("新增员工，参数：{}",emp);
        empService.addEmp(emp);
        if (emp.getId()== null) {
            return Result.error("新增失败");
        }
        return Result.success();
    }
    @LogOperation
    @DeleteMapping
    public Result deleteEmpById(@RequestParam Integer... ids)
    {
        log.info("删除员工，参数：{}",ids);
        int i = empService.deleteEmpById(ids);
        if (i==0)Result.error("删除失败");
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findEmpById(@PathVariable Integer id)
    {
        log.info("查询员工，参数：{}",id);
        Emp emp = empService.findEmpById(id);
        if (emp==null)return Result.error("查询失败!");
        log.info("查询员工，结果：{}",emp);
        return Result.success(emp);
    }
    @GetMapping("/list")
    public Result findEmpList1()
    {
        EmpQueryParam param = new EmpQueryParam();
        List<Emp> empList = empService.findEmpList1(param);
        return Result.success(empList);
    }
    @LogOperation
    @PutMapping
    public Result updateEmpById(@RequestBody Emp emp)
    {
        log.info("修改员工，参数：{}",emp);
        int i = empService.updateEmp(emp);
        if (i==0)return Result.error("修改失败");
        return Result.success();
    }
}
